package com.serkol.driving.learning.api;

import com.serkol.bean.MyPage;
import com.serkol.bean.Result;
import com.serkol.constant.ResultConstant;
import com.serkol.driving.learning.entity.QuestionBank;
import com.serkol.driving.learning.handler.QuestionMapResult;
import com.serkol.driving.learning.service.QuestionBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("api/question")
@RestController
public class QuestionApiController {
    @Autowired
    private QuestionBankService service;
    @GetMapping("/findByList")
    public Result findByList(@RequestParam(name="pageSize", defaultValue = "10") int pageSize, @RequestParam(name="page", defaultValue = "0") int page){
        Result result = new Result();
        MyPage list = service.findByList(pageSize, page);
        if (list.getSize()>0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody QuestionBank data) {
        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {
            flag = service.updateById(data);
        } else {
            flag = service.save(data);
        }
        if (flag) {
            return Result.ok();
        } else {
            return Result.failure();
        }

    }

    @GetMapping("/findByExamList")
    public Result findByExamList(@RequestParam(name="langId", defaultValue = "0") int langId,@RequestParam(name="subject", defaultValue = "0") int subject, @RequestParam(name="category", defaultValue = "0") int category){
        Result result = new Result();
        QuestionMapResult list = service.findByExamList(langId,subject,category);
        if (list.getTotal()>0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }


    @GetMapping("/findByLearnList")
    public Result findByLearnList(@RequestParam(name="langId", defaultValue = "0") int langId,@RequestParam(name="type", defaultValue = "0") int type,@RequestParam(name="subject", defaultValue = "0") int subject, @RequestParam(name="category", defaultValue = "0") int category){
        Result result = new Result();
        QuestionMapResult list = service.findByLearnList(langId,type,subject,category);
        result.setData(list);
        if (list.getTotal()>0) {
            result.setStatus(ResultConstant.SUCSESS);

        } else {

            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    @GetMapping("/findByExamAllList")
    public Result findByExamAllList(@RequestParam(name="pageSize", defaultValue = "10") int pageSize, @RequestParam(name="page", defaultValue = "0") int page,@RequestParam(name="langId", defaultValue = "0") int langId,@RequestParam(name="subject", defaultValue = "0") int subject, @RequestParam(name="category", defaultValue = "0") int category){
        Result result = new Result();
        MyPage list = service.findByExamAllList(pageSize,page,langId,subject,category);
        if (list.getTotal()>0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    //findByExamAllList

}
